package server;

import java.io.PrintWriter;
import java.net.URLEncoder;

import server.IDInstance.FileTypes;

public class IndexConstructor 
{
	private static void writeHeader(PrintWriter pw)
	{
		pw.println("<h1><span class=\"title\">InstaDir file server</span></h1><hr/>");
	}
	
	private static void writeFooter(PrintWriter pw)
	{
		pw.println("<hr/><h4>InstaDir v0.1 by <a href=\"https://twitter.com/LeBurek\" target=\"_blank\">TheBurek</a></h4><br/>");
	}
	
	private static String getHRSize(long size, boolean newline)
	{
		String sep = " ";
		if(newline) sep = "<br/>";
		if(size < 1024)
		{
			return size + sep + "<b>B</b>";
		}
		else if(size < 1024 * 1024)
		{
			return (size / 1024) + "." + (((size * 10) / 1024) % 10) + sep + "<b>kB</b>";
		}
		else if(size < 1024 * 1024 * 1024)
		{
			return (size / (1024 * 1024)) + "." + (((size * 10) / (1024 * 1024)) % 10) + sep + "<b>MB</b>";
		}
		else
		{
			return (size / (1024 * 1024 * 1024)) + "." + (((size * 10) / (1024 * 1024 * 1024)) % 10) + sep + "<b>GB</b>";
		}
	}
	
	@SuppressWarnings("deprecation")
	public static void constructIndex(IDInstance.IDFile fileRoot, PrintWriter pw)
	{
		Style.getInstance().dumpHeader(pw);
		writeHeader(pw);
		
		if(fileRoot.type == FileTypes.Folder)
		{
			pw.println("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
			
			if(fileRoot.parent != null)
			{
				pw.print("<tr class=\"even\"><td class=\"icon\">");
				pw.print("<a href=\"/dir/" + URLEncoder.encode(fileRoot.parent.relPath) + "\"><img src=\"/res/icon-9.png\" /></a></td><td class=\"name\">");
				pw.print("<a href=\"/dir/" + URLEncoder.encode(fileRoot.parent.relPath) + "\">(go back)</a>");
				pw.println("</td><td class=\"size\"> </td></tr>");
			}
			
			boolean even = false;
			for(IDInstance.IDFile f : fileRoot.children)
			{
				if(even)
					pw.print("<tr class=\"even\">");
				else
					pw.print("<tr class=\"odd\">");
				
				if(f.dir)
				{
					//pw.println("[ " + f.name + " ]<br/>");
					pw.print("<td  class=\"icon\"><a href=\"/dir/" + URLEncoder.encode(f.relPath) + "\"><img src=\"/res/icon-8.png\" /></a></td><td class=\"name\">");
					
					//+ f.name + 
					pw.print("<a href=\"/dir/" + URLEncoder.encode(f.relPath) + "\">" + f.name + "</a>");
					
					pw.println("</td><td class=\"size\">" + f.size + ((f.size == 1) ? " item" : " items") + "</td></tr>");
				}
				else
				{
					//pw.println(f.name + " (" + f.type + ", " + f.size + " bytes)<br/>");
					//pw.println("<td>" + f.type.toString() + "</td><td>" + f.name + "</td><td class=\"size\">" + getHRSize(f.size) + "</td></tr>");
					//pw.println("<td><img src=\"/res/icon-1.png\" />"
					pw.print("<td class=\"icon\"><a href=\"/file/" + URLEncoder.encode(f.relPath) + "\"><img src=\"");
					switch(f.type)
					{
					case Archive: pw.print("/res/icon-2.png"); break;
					case Audio:  pw.print("/res/icon-6.png"); break;
					case Executable: pw.print("/res/icon-3.png"); break;
					case Folder: pw.print("/res/icon-8.png"); break;
					case Image: pw.print("/res/icon-7.png"); break;
					case Text: pw.print("/res/icon-4.png"); break;
					case Unknown: pw.print("/res/icon-1.png"); break;
					case Video: pw.print("/res/icon-5.png"); break;
					}
					pw.print("\" /></a></td><td class=\"name\">");
					//+ f.name + 
					pw.print("<a href=\"/file/" + URLEncoder.encode(f.relPath) + "\">" + f.name + "</a>");
					pw.println("</td><td class=\"size\">" + getHRSize(f.size, true) + "</td></tr>");
				}
				even = !even;
			}
			pw.println("</table>");
		}
		else
		{
			pw.println("<div align=\"center\">");
			pw.print("<br/><img src=\"");
			switch(fileRoot.type)
			{
			case Archive: pw.print("/res/icon-2.png"); break;
			case Audio:  pw.print("/res/icon-6.png"); break;
			case Executable: pw.print("/res/icon-3.png"); break;
			case Folder: pw.print("/res/icon-8.png"); break;
			case Image: pw.print("/res/icon-7.png"); break;
			case Text: pw.print("/res/icon-4.png"); break;
			case Unknown: pw.print("/res/icon-1.png"); break;
			case Video: pw.print("/res/icon-5.png"); break;
			}
			pw.print("\" /><br/>");
			pw.println("<h2>" + fileRoot.name + "</h2>");
			//pw.println("(" + fileRoot.type.toString() + ", " + fileRoot.size + " bytes)<br/><br/>");
			pw.println("(" + getHRSize(fileRoot.size, false) + ")<br/><br/>");
			
			pw.print("<a href=\"/file/" + URLEncoder.encode(fileRoot.relPath) + "\">");
			pw.println("<span class=\"download\">DOWNLOAD</span>");
			pw.println("</a><br/><br/><br/>");
			
			pw.println("</div>");
		}
		
		writeFooter(pw);
		Style.getInstance().dumpFooter(pw);
	}
}
